package avayajtapirecepcion;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

import javax.telephony.Address;
import javax.telephony.Call;
import javax.telephony.CallEvent;
import javax.telephony.Connection;
import javax.telephony.ConnectionEvent;
import javax.telephony.JtapiPeer;
import javax.telephony.JtapiPeerFactory;
import javax.telephony.JtapiPeerUnavailableException;
import javax.telephony.MetaEvent;
import javax.telephony.MethodNotSupportedException;
import javax.telephony.Provider;
import javax.telephony.ProviderEvent;
import javax.telephony.ProviderListener;
import javax.telephony.ResourceUnavailableException;
import javax.telephony.Terminal;
import javax.telephony.TerminalConnection;
import javax.telephony.callcontrol.CallControlCall;
import javax.telephony.callcontrol.CallControlConnectionEvent;
import javax.telephony.callcontrol.CallControlConnectionListener;

/**
 * 
 * @author AIDALIT
 */
public class Recepcion {

	String[] services;
	
	String pass = "Salud_Integral1";
	Address addr;
	Provider myprovider;
	JtapiPeer peer;
	String propiedades = "marcador.conf";
	String us;
	ListenerLlamada listener;
	ListenerServer listenerserver;
	String url;
	String extension;

	public Recepcion() {
		
		if (!SystemTray.isSupported()) {
			System.out.println("SystemTray is not supported");
			return;
		}

		SystemTray tray = SystemTray.getSystemTray();
		Toolkit toolkit = Toolkit.getDefaultToolkit();

		Image image = toolkit.getImage(Recepcion.class.getClassLoader()
				.getResource("telefono.jpg"));
		PopupMenu menu = new PopupMenu();
		
		/*MenuItem closeItem = new MenuItem("Cerrar");
		
		closeItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
		menu.add(closeItem);*/
		TrayIcon icon = new TrayIcon(image, "Monitor Avaya", menu);
		icon.setImageAutoSize(true);
		
		try {
			tray.add(icon);
		} catch (AWTException ex) {
			ex.printStackTrace();
		}
	}
	
//metodo getMD5
	
	public String getMD5(String input) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] messageDigest = md.digest(input.getBytes());
			BigInteger number = new BigInteger(1, messageDigest);
			String hashtext = number.toString(16);
			while (hashtext.length() < 32) {
				hashtext = "0" + hashtext;
			}
			return hashtext;
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}

//metodo de conexion	
	
	public void conexion() throws Excepciones {
				
		this.myprovider = this.peer.getProvider(this.services[0] + ";login="
				+ this.us + ";passwd=" + this.pass);

		while (this.myprovider == null) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.myprovider = this.peer.getProvider(this.services[0]
					+ ";login=" + this.us + ";passwd=" + this.pass);
		}

	}

	public void asociarExtension() throws Excepciones
	{
		try
		{
		addr.removeCallListener(listener);
		}
		catch(Exception e)
		{
			
		}
		try {
			this.addr = this.myprovider.getAddress(extension);
			this.addr.addCallListener(listener);

		} catch (Exception e) {
			throw new Excepciones("No hay extension disponible");
		}

	}
	
	
	public void recepcion() throws Excepciones {
		
		Properties misPropiedades = new Properties();
		InputStream input = Recepcion.class.getClassLoader()
				.getResourceAsStream(propiedades);
		
		if (input == null)
			throw new Excepciones("No se encontro archivo");
		try {
			misPropiedades.load(input);
		} catch (Exception ex) {
			throw new Excepciones("No se cargo el archivo de propiedades");
		}
		
		this.us = misPropiedades.getProperty("user_cti");
		String pas = misPropiedades.getProperty("password_cti");
		extension = misPropiedades.getProperty("extension");
		url = misPropiedades.getProperty("url");

		if (this.us == null)
			throw new Excepciones("No hay Usuario");
		if (pas == null || pas.equals(this.getMD5(this.pass)))
			throw new Excepciones("No hay Password");
		if (extension == null)
			throw new Excepciones("No hay Extension");
		if (url == null)
			throw new Excepciones("No hay Url");
		System.setProperty("com.avaya.jtapi.tsapi.servers","192.168.30.13:450");
		
		try {	
			this.peer = JtapiPeerFactory.getJtapiPeer(null);
		} catch (JtapiPeerUnavailableException e) {
			throw new Excepciones("No hay servicio disponible");
		}

		this.services = this.peer.getServices();
		if (this.services.length == 0) {
			throw new Excepciones("No hay TLINK disponible en AES");

		}
		
		listener = new ListenerLlamada(url);
		listenerserver=new ListenerServer(this);
		
		this.conexion();
		try {
			this.myprovider.addProviderListener(listenerserver);
		} catch (ResourceUnavailableException e) {
			// TODO Auto-generated catch block
			throw new Excepciones("Recurso no disponible");
		} catch (MethodNotSupportedException e) {
			// TODO Auto-generated catch block
			throw new Excepciones("Listener de proveedor no soportado");
		}
		this.asociarExtension();

		
	}
}
